////////////////////////////////////////////////
// File: SysroleController.java
// Description: Enter summary here after generation.
// Date: 2016-09-23 09:09:34
// Web: http://www.elanzhou.com/
////////////////////////////////////////////////
package com.mvc.controller;
import java.io.IOException;
import java.util.List;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mvc.entity.Meminfo;
//import com.mvc.entity.User;
//import com.mvc.entity.UserDetail;
import com.mvc.entity.Sysrole;
import com.mvc.service.SysroleService;
import com.pub.MyJson;
import com.pub.Page;
import com.aspose.cells.Cells;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
@Controller
@RequestMapping("Sysrole")
public class SysroleController {
	protected final transient Log logger = LogFactory.getLog(SysroleController.class);
	@Autowired
	private SysroleService sysroleService;

	public SysroleController() {
	}

	/*列表*/
	@RequestMapping("Index.do")
	public String Index(HttpSession session,ModelMap modelMap, String pageNo, String Ajax) {
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		
		Page page = new Page();
		page.setPageNox(pageNo);
		page.setPageSize(15);
		String hql = "select a from Sysrole a order by a.ID desc";
		List list = sysroleService.getSysroleList(hql, page);
		String counthql = "select count(*) from Sysrole a";
		page.setPageCount(sysroleService.getCount(counthql, page));
		modelMap.put("page", page);
		modelMap.put("list", list);
		if (Ajax != null && Ajax.equals("Ajax")) {
			return "/Sysrole/List";
		} else {
			return "/Sysrole/Index";
		}

	}
	/*添加*/ 
	@RequestMapping("Add.do")
	public String Add(ModelMap modelMap,HttpSession session)
	{
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		
		return "/Sysrole/Add";
	}
	/*保存*/
	@RequestMapping("Save.do")
	public void Save(HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, HttpSession session) {
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		
		try {
			
			Sysrole  sysrole= new Sysrole();
			boolean tag=true;
		    String Msg="信息保存成功";
             
               if(tag){
                              
               String FieldRolename=(String)request.getParameter("Rolename");
             
               if(((FieldRolename!=null)&(!FieldRolename.trim().equals("")))){
            	   FieldRolename=FieldRolename.trim();
                    sysrole.setRolename(FieldRolename);
                  
        		}else
        		{
        			tag=false;
        			Msg="角色名称不能为空！";
        		}
        		}
	         
             
               if(tag){
                              
               String FieldChannelid=(String)request.getParameter("Channelid");
             
               if(((FieldChannelid!=null)&(!FieldChannelid.trim().equals("")))){
            	   FieldChannelid=FieldChannelid.trim();
                    sysrole.setChannelid(FieldChannelid);
                  
        		}else
        		{
        			tag=false;
        			Msg="栏目不能为空！";
        		}
        		}
	         
            
            MyJson mj=new MyJson();
              if(tag){
                     sysroleService.Save(sysrole);	 
        			 mj.setStatusCode(200);
        		 }else
        		 {
        			 mj.setStatusCode(300);
        		 }
             mj.setMessage(Msg);
             mj.setForwardUrl("/Sysrole/Index.do");
             mj.setCallbackType("forward");
             String str=mj.getJson();
			 response.getWriter().print(str);
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
	} 
    
	/*编辑*/
	@RequestMapping("Edit.do")
	public String Edit(HttpSession session,HttpServletRequest request,ModelMap modelMap)
	{
		String ID=request.getParameter("ID");
		Sysrole sysrole=(Sysrole)sysroleService.getSysrole(ID);
		modelMap.put("sysrole", sysrole);
		return "/Sysrole/Edit";
	}
    
    /*编辑保存*/
	@RequestMapping("EditSave.do")
	public void EditSave(HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap,HttpSession session) {
	//		User user=(com.mvc.entity.User)session.getAttribute("SessionUser");	
	//		UserDetail ud=(com.mvc.entity.UserDetail)session.getAttribute("SessionUserDetail");
		try {
			String Id=(String)request.getParameter("ID");
			Sysrole  sysrole=(Sysrole)sysroleService.getSysrole(Id);
			boolean tag=true;
		    String Msg="信息修改成功";
             
            if(tag){
               
               String FieldRolename=(String)request.getParameter("Rolename");
             
               if(((FieldRolename!=null)&(!FieldRolename.trim().equals("")))){
            	   FieldRolename=FieldRolename.trim();
                    sysrole.setRolename(FieldRolename);
                  
        		}else
        		{
        			tag=false;
        			Msg="角色名称不能为空！";
        		}
        		}
	         
             
            if(tag){
               
               String FieldChannelid=(String)request.getParameter("Channelid");
             
               if(((FieldChannelid!=null)&(!FieldChannelid.trim().equals("")))){
            	   FieldChannelid=FieldChannelid.trim();
                    sysrole.setChannelid(FieldChannelid);
                  
        		}else
        		{
        			tag=false;
        			Msg="栏目不能为空！";
        		}
        		}
	         
            
            MyJson mj=new MyJson();
              if(tag){
                     sysroleService.EditSave(sysrole);	 
        			 mj.setStatusCode(200);
        		 }else
        		 {
        			 mj.setStatusCode(300);
        		 }
             mj.setMessage(Msg);
             mj.setForwardUrl("/Sysrole/Index.do");
             mj.setCallbackType("forward");
             String str=mj.getJson();
			 response.getWriter().print(str);
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
	} 
    
	/*删除*/
	@RequestMapping("Del.do")
	public void Del(HttpSession session,HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap) {
             String ID=request.getParameter("ID");
             sysroleService.Del(ID);
             String Msg="数据删除成功";
             MyJson mj=new MyJson();
             mj.setStatusCode(200);
             mj.setMessage(Msg);
             mj.setForwardUrl("/Sysrole/Index.do");
             mj.setCallbackType("forward");
             String str=mj.getJson();
             try {
                  response.getWriter().print(str);
                 } catch (IOException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
                 }
	      }
          	/*编辑*/
	@RequestMapping("Detail.do")
	public String Detail(HttpSession session,HttpServletRequest request,ModelMap modelMap)
	{
		String ID=request.getParameter("ID");
		Sysrole sysrole=(Sysrole)sysroleService.getSysrole(ID);
		modelMap.put("sysrole", sysrole);
		return "/Sysrole/Detail";
	}
    
 
}
